<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Db;
use think\facade\Log;

class BookApplyDetail extends Common
{
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field('bad.*, vd.bmmc as dep, vsp.zymc as specialty, vc.plan_class_name as class, vdr.name as dir, u.mobile, u.truename as teacher')
            ->alias('bad')
            ->join('zl_book_apply ba', 'ba.id = bad.applyid')
            ->leftjoin('zl_view_get_depts vd', 'vd.bmdm = ba.dep_id')
            ->leftjoin('zl_view_get_specicalties vsp', 'vsp.zydm = ba.specialty_id and vsp.bmdm = ba.dep_id ')
            ->leftjoin('zl_view_get_classes vc', 'vc.plan_class_id = ba.class_id and vc.bmdm = ba.dep_id and vc.zydm = ba.specialty_id ')
            ->leftjoin('zl_view_get_dirs vdr', 'vdr.id = ba.dir')
            ->leftjoin('zl_user u', 'u.id = ba.teacherid')
            ->where($tableWhere['where'])
            ->order($tableWhere['order'])
            ->paginate($limit);
             
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;
        return $re;
    }

    public function getStudentApplyInfo($year, $season, $xh)
    {
        $info = $this->field('ba.grade, bad.id')
            ->alias('bad')
            ->join('zl_book_apply ba', 'ba.id = bad.applyid')
            ->where(['ba.year' => $year, 'ba.season' => $season, 'bad.xh' => $xh])
            ->find();

        return $info;
    }

    //where搜索条件
    protected function tableWhere($post)
    {       
        $where = [];
        if(isset($post['applyid']) && $post['applyid'] != ""){
            $where[] = ['applyid', 'eq', $post['applyid']];
        }
        if(isset($post['xh']) && $post['xh'] != ""){
            $where[] = ['xh', 'like', '%'.$post['xh'].'%'];
        }
        if(isset($post['xm']) && $post['xm'] != ""){
            $where[] = ['xm', 'like', '%'.$post['xm'].'%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['id ASC, is_qs'];
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as &$val){
            $val['is_qs'] = $val['is_qs'] == 0?'未签收':'已签收';  
        }
        return $list;
    }

    public function getDrawStatus($applyid)
    {
        $info = $this->field('id') 
            ->where(['applyid' => $applyid, 'is_qs' => 1])
            ->find();

        if(!$info) {
            return false;
        } else {
            return true;
        }
    }
    

    // 设置csv header
    public function csvHeader()
    {
        return [
            [
                'id' => 'xh',
                'desc' => '学号',
            ],
            [
                'id' => 'xm',
                'desc' => '姓名',
            ],
            [
                'id' => 'dept',
                'desc' => '部/院',
            ],
            [
                'id' => 'specialty',
                'desc' => '专业',
            ],
            [
                'id'   => 'class',
                'desc' => '班级',
            ],
            [
                'id' => 'dir',
                'desc' => '方向',
            ],
            [
                'id' => 'mobile',
                'desc' => '辅导员手机号',
            ],
            [
                'id' => 'teacher',
                'desc' => '辅导员',
            ],
        ];
    }


    // 获取导出数据
    public function getBookApplyDetailExportData($header, $post)
    {
        $result   = [
            'status' => false,
            'data'   => [],
            'msg'    => '无可导出明细'
        ]; 
        $where = [];
        if(isset($post['applyid']) && $post['applyid'] != ""){
            $where[] = " applyid = " . $post['applyid'];
        }
        if(isset($post['xh']) && $post['xh'] != ""){
            $where[] = " xh like '%" . $post['xh'] . "%'";
        }
        if(isset($post['xm']) && $post['xm'] != ""){
            $where[] = " xm like '%" . urldecode($post['xm']) . "%'";
        }

        $whereCondition = 'where 1=1 ';
        $index = 0;
        
        if(count($where) > 0) {
            $whereCondition = $whereCondition . ' and ';
        }

        foreach ($where as $value) {
            $index++;
            if($index == count($where)) {
                $whereCondition = $whereCondition . $value;
            } else {
                $whereCondition = $whereCondition . $value . ' and ';
            }
        } 
        
        $sql = "select bad.*, case when bad.is_qs = 1 then '已签收' when bad.is_qs = 0 then '未签收' end as status, ba.year, vs.season_name as season, 
        ba.dep_id, vd.bmmc as dept, ba.specialty_id, vsp.zymc as specialty, ba.class_id, vc.plan_class_name as class, vdr.name as dir, u.mobile, u.truename as teacher
        from zl_book_apply_detail bad join zl_book_apply ba on ba.id = bad.applyid 
        left join zl_view_get_seasons vs on vs.season_id = ba.season 
        left join zl_view_get_depts vd on vd.bmdm = ba.dep_id 
        left join zl_view_get_specicalties vsp on vsp.zydm = ba.specialty_id and vsp.bmdm = ba.dep_id 
        left join zl_view_get_classes vc on vc.plan_class_id = ba.class_id and vc.bmdm = ba.dep_id and vc.zydm = ba.specialty_id 
        left join zl_view_get_dirs vdr on vdr.id = ba.dir 
        left join zl_user u on u.id = ba.teacherid " . $whereCondition;
        
        $bookapplydetailData = Db::query($sql);

        $bookapplydetailDataInfo = array(); 
        foreach ($bookapplydetailData as $key => $val) {
            $bookapplydetailDataInfo[] = $val;
        }

        if (count($bookapplydetailData) > 0) {
            $result['status'] = true;
            $result['msg']    = '导出成功';
            $result['data']   = $bookapplydetailDataInfo;
            return $result;
        } else {
            //失败，导出失败
            return $result;
        }
    }

    public function getApplyXhList($classid)
    {
        $list = $this->field('bad.xh')
            ->alias('bad')
            ->join('zl_book_apply ba', 'ba.id = bad.applyid')
            ->where(['class_id' => $classid, 'is_qs' => 0])
            ->select();

        $xhArray = [];
        foreach($list as $key) {
            $xhArray[] = $key['xh'];
        }

        return $xhArray;
    }
}